package com.example.viwer3d.control;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.example.viwer3d.model.Cubo;
import com.example.viwer3d.model.Object3D;
import com.example.viwer3d.model.Piramide;
import com.example.viwer3d.model.Square;
import com.example.viwer3d.model.Triangle;

import android.content.Context;
import android.graphics.Color;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;

public class MyGLRenderer implements Renderer {
	Triangle triangle;
	Square quad;
	Piramide piramide;
	Cubo cubo;
	public static float rotacionaX = 0.0f;
	public static float rotacionaY = 0.0f;
	public static float rotacionaZ = 0.0f;
	public static String objetoEmAcao = null;
	public static float zoomMigueh = -10.0f;
	public static float posicaoX = 0.0f;
	public static float posicaoY = 0.0f;
	public float red = 0.0f;
	public float green = 0.0f;
	public float blue = 0.0f;
	public float alpha = 1.0f;

	public MyGLRenderer(Context context) {
		triangle = new Triangle();
		quad = new Square();
		piramide = new Piramide();
		cubo = new Cubo();
	}

	// Call back when the surface is first created or re-created
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glClearColor(red, green, blue, alpha); // Set color's clear-value to
													// black
		gl.glClearDepthf(1.0f); // Set depth's clear-value to farthest
		gl.glEnable(GL10.GL_DEPTH_TEST); // Enables depth-buffer for hidden
											// surface removal
		gl.glDepthFunc(GL10.GL_LEQUAL); // The type of depth testing to do
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); // nice
																		// perspective
																		// view
		gl.glShadeModel(GL10.GL_SMOOTH); // Enable smooth shading of color
		gl.glDisable(GL10.GL_DITHER); // Disable dithering for better
										// performance
		// You OpenGL|ES initialization code here
		// ......
	}

	// Call back after onSurfaceCreated() or whenever the window's size changes
	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		if (height == 0)
			height = 1; // To prevent divide by zero
		float aspect = (float) width / height;
		// Set the viewport (display area) to cover the entire window
		gl.glViewport(0, 0, width, height);
		// Setup perspective projection, with aspect ratio matches viewport
		gl.glMatrixMode(GL10.GL_PROJECTION); // Select projection matrix
		gl.glLoadIdentity(); // Reset projection matrix
		// Use perspective projection
		GLU.gluPerspective(gl, 45, aspect, 0.1f, 100.f);
		gl.glMatrixMode(GL10.GL_MODELVIEW); // Select model-view matrix
		gl.glLoadIdentity(); // Reset
	}

	ObjectComand obComm = new ObjectComand();

	@Override
	public void onDrawFrame(GL10 gl) {
		gl.glClearColor(red, green, blue, alpha);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		GLU.gluLookAt(gl, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, zoomMigueh, 5, 0.0f,
				0.0f);
		// GLU.gluLookAt(gl, rotacionaX, rotacionaY, 10,rotacionaX, rotacionaY,
		// 0, rotacionaX, rotacionaY, 0);

		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();

		// gl.glOrthof(-1,0f, 1.0f, -1.0f, 1.0f, 30.0f);

		// gl.glLoadIdentity();
		gl.glTranslatef(posicaoX, posicaoY, zoomMigueh);

		gl.glRotatef(rotacionaX, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(rotacionaY, 0.0f, 1.0f, 0.0f);
		gl.glRotatef(rotacionaZ, 0.0f, 0.0f, 1.0f);

		Object3D objGenerico;

		if (objetoEmAcao != null) {
			objGenerico = obComm.InstanciaPorBotao(objetoEmAcao);
			objGenerico.draw(gl);
		}
		//
		/*
		 * gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -10f);
		 * gl.glScalef(zoomMigueh, zoomMigueh, zoomMigueh);
		 * gl.glRotatef(rotacionaX, 1.0f, 0.0f, 0.0f); gl.glRotatef(rotacionaY,
		 * 0.0f, 1.0f, 0.0f); gl.glRotatef(rotacionaZ, 0.0f, 0.0f, 1.0f);
		 * Piramide a= new Piramide(); a.draw(gl);
		 */

		//
	}
}